<template>
	<view class="container">
		<view class="content">
			<view class="other_box" v-if="userInfo.id != data.personIncomeInfo?.userId">
				<view class="lf lf-center lf-between">
					<view class="lf lf-center">
						<image class="other_accounts_img" :src="data.personIncomeInfo?.user?.avatar"></image>
						<view class="">
							<view class="other_name">{{ data.personIncomeInfo?.user?.realName }} |
						{{ data.personIncomeInfo?.user?.nickName }}</view>
							<view class="xjs-size-24 account_num">
								手机号：{{ data.personIncomeInfo?.user?.phone }}
							</view>
						</view>
					</view>
					<!-- <view class="oldBalanceDetails" @tap="oldBalanceDetails" v-if="userInfo.id != data.personIncomeInfo?.userId">
						旧余额
						<uni-icons type="right" color="#FF6084" size="16"></uni-icons>
					</view> -->
				</view>
			</view>
			<view class="lf lf-between lf-center xjs-mt-64 xjs-ml-32 xjs-mr-32" v-if="studioListName.length>1&&userInfo.id == data.personIncomeInfo?.userId">
				<view class="xjs-size-32 xjs-weight-bold" @click="openStudioPopup" v-if="studioListName.length>1">
					{{studioText}}
					<uni-icons :type="downUp" size="16"></uni-icons>
				</view>
				<view> </view>
				<view class="" @tap="goBankCard" v-if="userInfo.id == data.personIncomeInfo?.userId">
					<text class="withdrawal_text">提现银行卡</text>
					<uni-icons type="right" color="#FF6084" size="13"></uni-icons>
				</view>
			</view>
			<view class="top_box">
				<view class="xjs-ml-32">
					<view class="xjs-size-26 xjs-pt-64 xjs-mb-16">可提现金额(元)</view>
					<view class="xjs-size-64 xjs-weight-bold">{{data.userBalance.balance}}</view>
					<view class="xjs-size-26 xjs-mt-64">
						提现中(元)：¥ {{data.userBalance.withdrawBalance}}
						<text class="xjs-ml-24 xjs-mr-24">|</text>
						已提现(元)：¥ {{data.userBalance.totalBalance}}</view>
				</view>
				<view class="goWithdrawal_box" v-if="userInfo.id == data.personIncomeInfo?.userId"  @tap="goWithdrawal">
					<view class="goWithdrawal">
						<text>去提现</text>
						<uni-icons type="right" color="#FF6084" size="14"></uni-icons>
					</view>
				</view>
			</view>
			<view class="lf lf-between lf-center xjs-mt-64 xjs-ml-32 xjs-mr-32">
				<view class="xjs-size-32 xjs-weight-bold" @click="openApplyPetPopup">
					{{applyPetText}}
					<uni-icons :type="downUp" size="16"></uni-icons>
				</view>
				<view class="" @tap="goWithdrawalRecord">
					<text class="withdrawal_text">提现记录</text>
					<uni-icons type="right" color="#FF6084" size="13"></uni-icons>
				</view>
			</view>
			<view class="main" v-if="!data.loading">
				<view class="list_box" @click="goDetails(item.id)" v-for="item in data.list">
					<view>
						<view class="xjs-size-32" v-if="item.type==1">服务订单——服务收益</view>
						<view class="xjs-size-32" v-else-if="item.type==2">商城订单——推广收益</view>
						<view class="xjs-size-32" v-else-if="item.type==3">提现成功</view>
						<view class="xjs-size-32" v-else>服务订单——推广收益</view>
						<view class="revenue xjs-pt-16">{{item.createTime}}</view>
					</view>
					<view class="time_text"> ¥{{item.balance}}</view>
					<!-- <view class="time_text expenditures" v-else>
						-  ¥ {{item.amount}}
					</view> -->
				</view>
			</view>
			<view class="noRecord" v-else>
				暂无记录～
			</view>
		</view>
		<uni-popup ref="popupRef" type="bottom" :is-mask-click="false" border-radius="24rpx"
			background-color="#fff">
			<view class="apply_pet">
				<view class="apply_pet_list">
					<view :class="data.experienceType === applyPetItem.type ? 'apply_pet_act': ''"
						v-for="(applyPetItem,applyPetIndex) in applyPet" :key="applyPetIndex"
						@tap="applyPetBtn(applyPetItem)">{{applyPetItem.name}}
					</view>
				</view>
				<view @tap="closeApplyPetPopup" class="xjs-btn-primary">取消</view>
			</view>
		</uni-popup>
		<uni-popup ref="popupRef1" type="bottom" :is-mask-click="false" border-radius="24rpx"
			background-color="#fff">
			<view class="apply_pet">
				<view class="apply_pet_list">
					<view :class="data.studioType === item.id ? 'apply_pet_act': ''"
						v-for="(item,index) in studioListName" :key="index"
						@tap="studioBtn(item)">{{item.studioName}}
					</view>
				</view>
				<view @tap="closeStudioPopup" class="xjs-btn-primary">取消</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup lang="ts">
	import { reactive , ref ,getCurrentInstance} from 'vue';
	import { onShow,onReachBottom,onNavigationBarButtonTap,onLoad,onReady } from '@dcloudio/uni-app'
	import { useUserStoreToRefs } from "@/store/useUserStore";
	let { userInfo } = useUserStoreToRefs();
	import api from '@/api'
	const applyPet = ref([
		{ name: '全部', type: '-1' },
		{ name: '服务订单——推广收益', type: '0' },
		{ name: '服务订单——服务收益', type: '1' },
		{ name: '商城订单——推广收益', type: '2' },
		{ name: '提现成功', type: '3' },
	])
	const studioListName = ref([])
	const popupRef1 = ref()
	const studioText = ref('')
	const downUp1 = ref('down')
	const studioBtn = (item) => {
		studioText.value = item.studioName
		data.studioType = item.id
		downUp.value = 'down'
		popupRef1.value.close()
	}
	const openStudioPopup = () => {
		downUp.value = 'up'
		popupRef1.value.open()
	}
	const closeStudioPopup = () => {
		downUp.value = 'down'
		popupRef1.value.close()
	}
	
	const popupRef = ref()
	const applyPetText = ref('全部')
	const downUp = ref('down')
	const applyPetBtn = (item) => {
		applyPetText.value = item.name
		data.experienceType = item.type
		downUp.value = 'down'
		data.list =[]
		data.page = 1
		getList()
		popupRef.value.close()
	}
	const openApplyPetPopup = () => {
		downUp.value = 'up'
		popupRef.value.open()
	}
	const closeApplyPetPopup = () => {
		downUp.value = 'down'
		popupRef.value.close()
	}
	let data = reactive<any>({
		list:[],
		page:1,
		limit:10,
		status : '0',
		loading: false,
		finished: false,
		experienceType:'-1',
		studioType:'',
		userBalance:{},
		userStudio:[],
		studioId:'',
		userId:'',
		personIncomeInfo:{}
	})	
	const { proxy } = getCurrentInstance();
	const toggleButton = () => {
		if (data.userId != userInfo.value.id) {
			const webView = proxy.$scope.$getAppWebview();
			webView.setStyle({
				titleNView: {
					buttons: []
				}
			});
		}
	};
	onNavigationBarButtonTap(() => {
		uni.navigateTo({
			url:'/pages/myStudio/studio/balanceDetails/index?userId='+data.userId
		});
	});
	const goDetails =(id: string)=>{
		uni.navigateTo({
			url:'/pages/income/balanceDetails/index?id='+id
		})
	}
	const goWithdrawal =()=>{
		uni.navigateTo({
			url:`/pages/income/initiateWithdrawal/index?type=2&studioId=${data.studioId}&userId=${data.userId}`
		})
	}
	const goBankCard =()=>{
		uni.navigateTo({
			url:'/pages/myStudio/studio/bankCardManagement/index'
		})
	}
	const goWithdrawalRecord =()=>{
		uni.navigateTo({
			url:'/pages/income/personalWithdrawalRecord/index?type=2&userId='+data.userId
		})
	}
	const getUserBalance = async () => {
		const res : any = await api({
			method: 'get',
			url: `/mall/studio/balance/getUserBalance/${data.studioId}/${data.userId}`
		})
		data.userBalance = res.data
	}

	const getUserIncome = async () => {
		const res : any = await api({
			method: "get",
			url: `/mall/studio/balanceRecord/getUserIncome/${data.studioId}/${data.userId}`
		})
		data.personIncomeInfo = res.data.petSitterTeamUserVO
	}
	const getUserStudioBalanceList = async () => {
		const res : any = await api({
			method: 'get',
			url: `/mall/studio/balance/getUserStudioBalanceList/${data.userId}`
		})
		studioListName.value = res.data
		studioText.value = res.data[0].studioName
		data.studioType = res.data[0].id
		data.studioId = res.data[0].studioId
	}
	
	const getList =async()=>{
		uni.showLoading({
		  title: '加载中',
		  mask: true
		}) 
		const res : any = await api({
			method: 'get',
			url: `/mall/studio/studioBalanceChange/page`,
			data:{
				balanceId:data.userBalance.id,
				type:data.experienceType,
				page:data.page,
				limit:data.limit
			}
		})
		data.extra = res.data.extra
		if (res.data.records.length != 0) {
			data.list = data.list.concat(res.data.records);
			data.finished = true
		}else{
			data.finished = false
		}
		if (data.list.length <= 0) {
			data.loading = true
		}
		uni.hideLoading()
	}
	onReachBottom(()=>{
		if (data.finished && !data.loading) {
			data.page++
			getList()
		}
	})
	onReady(() => {
		toggleButton()
	})
	onLoad((e) => {
		data.userId = e.userId
	})
	onShow(async() => {
		await getUserStudioBalanceList()
		await getUserIncome()
		await getUserBalance()
		uni.hideLoading()
		data.list =[]
		data.page = 1
		getList()
	})
</script>

<style lang="scss" scoped>
	.other_box{
		background-color: #ffffff;
		border-radius: 24rpx;
		width: 672rpx;
		margin: 32rpx auto;
		padding-bottom: 32rpx;
		.other_accounts_img{
			margin: 32rpx 32rpx 0 24rpx;
			width: 84rpx;
			height: 84rpx;
			border-radius: 42rpx;
		}
		.other_name{
			margin-top: 20rpx;
			font-size: 30rpx;
		}
		.oldBalanceDetails{
			color: #FF6084;
			font-size: 26rpx;
			margin-right: 32rpx;
		}
	}
	.apply_pet {
		padding: 0 32rpx;
		box-sizing: border-box;
		line-height: 1;
	
		.apply_pet_title {
			padding-top: 48rpx;
			padding-bottom: 24rpx;
			font-size: 36rpx;
			color: #1A1A1A;
			font-weight: bold;
			text-align: center;
	
			text {
				margin-right: 100rpx;
			}
		}
	
		.apply_pet_list {
			padding-bottom: 48rpx;
	
			view {
				margin-top: 40rpx;
				width: 686rpx;
				height: 92rpx;
				line-height: 92rpx;
				box-sizing: border-box;
				background-color: #F5F6F7;
				text-align: center;
				font-size: 30rpx;
				color: #5C5E5E;
				border-radius: 400rpx;
			}
	
			.apply_pet_act {
				border: 2rpx solid #FF6084;
				background-color: #fff;
				color: #FF6084;
			}
		}
	}
	.lf {
		display: flex;
	}
	.noRecord{
		margin-top: 48%;
		text-align: center;
		font-size: 32rpx;
	}
	.lf-between {
		justify-content: space-between;
	}
	.lf-center {
		align-items: center;
	}
	.lf-around {
		justify-content: space-around;
	}
	.container {
		padding-bottom: var(--safe-area-bottom);
		// #ifdef APP
		padding-bottom: calc(24rpx + var(--safe-area-bottom));
		// #endif
		box-sizing: border-box;
	}
	.expenditures{
		color: #6363FF !important;
	}
	.content {
		.top_box{
			width: 686rpx;
			height: 324rpx;
			background: linear-gradient( 105deg, #FF7D9E 0%, #FFB796 100%);
			border-radius: 24rpx;
			margin: 32rpx auto;
			color: #FFFFFF;
			position: relative;
		}
		.goWithdrawal_box{
			position: absolute;
			top:48rpx;
			right: 0;
			width: 176rpx;
			height: 76rpx;
			line-height: 76rpx;
			background: #FFFFFF;
			border-radius: 38rpx 0rpx 0rpx 38rpx;
			color: #FF6084;
			text-align: center;
		}
		.goWithdrawal{
			font-size: 28rpx;
		}
		.withdrawal_text{
			color: #FF6084;
			font-size: 26rpx;
		}
		.time_box {
			display: flex;
			justify-content: space-between;
			margin: 32rpx 32rpx 24rpx 32rpx;
			height: 96rpx;
			line-height: 96rpx;
			background: #FFFFFF;
			border-radius: 24rpx;
			padding: 0 32rpx;
		}
		.time_text {
			font-weight: bold;
			font-size: 32rpx;
		}
		.revenue {
			color: #7C807F;
			font-size: 26rpx;
		}
		.main{
			border-radius: 24rpx;
			background: #FFFFFF;
			margin: 32rpx;
		}
		.list_box{
			display: flex;
			justify-content: space-between;
			padding: 48rpx 0;
			border-bottom: 1rpx solid #E4E6E6;
			align-items: center;
			margin: 0 32rpx;
		}
		.list_box:last-child{
			border-bottom: 0rpx solid;
		}
	}
</style>